package com.kingwood.algorithm.test200;

import java.util.*;

// 中文分词模拟器
// https://fcqian.blog.csdn.net/article/details/134732074
public class ZhongWenFenCi {
    public static String getResult(String[] sentences, String[] words) {
        HashSet<String> wordSet = new HashSet<>(Arrays.asList(words));
        LinkedList<String> queue = new LinkedList<>(Arrays.asList(sentences));
        LinkedList<String> ans = new LinkedList<>();

        while (queue.size() > 0) {
            String sentence = queue.removeFirst();
            int r = sentence.length();
            for (; r > 0; r--) {
                String fragment = sentence.substring(0, r);
                if (wordSet.contains(fragment)) {
                    ans.addLast(fragment);
                    wordSet.remove(fragment); // 是否移除不确定

                    if (r < sentence.length()) {
                        // fragment 0-r范围已经匹配上了，剩下的范围继续去词库中匹配
                        queue.addFirst(sentence.substring(r));
                    }
                    break;
                }
            }

            // 没有在词库中找到对应的子串词汇，则输出单个字母
            if (r==0) {
                ans.add(sentence.charAt(0) + "");
                if (sentence.length() > 1) {
                    // 去除首字母后继续加入到队列中去查找
                    queue.addFirst(sentence.substring(1));
                }
            }
        }

        StringJoiner sj = new StringJoiner(",");
        ans.forEach(sj::add);
        return sj.toString();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] sentences = scanner.nextLine().split("[,.:]");
        String[] words = scanner.nextLine().split("[,.:]");

        String res = getResult(sentences, words);
        System.out.println(res);
    }
}
